package org.example.controller;

import org.example.model.MaterialRequest;
import org.example.service.MaterialRequestService;
import org.example.util.PageResponse;
import org.example.util.ResponseUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin   // 解决跨域问题
@RequestMapping("/api/materialRequest")
public class MaterialRequestController {
    private final MaterialRequestService materialRequestService;

    public MaterialRequestController(MaterialRequestService materialRequestService) {
        this.materialRequestService = materialRequestService;
    }

    // 添加物料申请
    @PostMapping
    public ResponseEntity<Void> addMaterialRequest(@RequestBody MaterialRequest request) {
        materialRequestService.addMaterialRequest(request);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    // 修改物料申请（路径ID需与请求体ID一致）
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateMaterialRequest(
            @PathVariable int id,
            @RequestBody MaterialRequest request) {
        if (id != request.getRequestId()) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

        boolean isUpdated = materialRequestService.updateMaterialRequest(request) > 0;
        return isUpdated
                ? new ResponseEntity<>(HttpStatus.OK)
                : new ResponseEntity<>(HttpStatus.NOT_FOUND); // 未找到对应申请
    }

    // 查询所有物料申请
    @GetMapping
    public ResponseEntity<List<MaterialRequest>> getAllMaterialRequests() {
        List<MaterialRequest> requests = materialRequestService.getAllMaterialRequests();
        return new ResponseEntity<>(requests, HttpStatus.OK);
    }

    // 按条件分页查询物料申请（支持物料名称模糊查询、审批状态精确匹配）
    @GetMapping("/search")
    public ResponseEntity<PageResponse<MaterialRequest>> searchMaterialRequests(
            @RequestParam(required = false) String materialName, // 可选参数
            @RequestParam(required = false) String approvalStatus, // 可选参数
            @RequestParam int page, // 前端页码（从1开始）
            @RequestParam int size // 每页大小
    ) {
        // 处理分页参数（前端页码转后端下标，从0开始）
        Pageable pageable = PageRequest.of(page <= 0 ? 0 : page - 1, size);
        Page<MaterialRequest> result = materialRequestService.searchMaterialRequests(
                materialName,
                approvalStatus,
                pageable
        );
        return ResponseEntity.ok(ResponseUtil.success(result.getTotalElements(), result.getContent()));
    }

    // 根据申请ID查询物料申请
    @GetMapping("/{id}")
    public ResponseEntity<MaterialRequest> getMaterialRequestById(@PathVariable Integer id) {
        MaterialRequest request = materialRequestService.getMaterialRequestById(id);
        return request != null
                ? new ResponseEntity<>(request, HttpStatus.OK)
                : new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    // 删除物料申请
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteMaterialRequest(@PathVariable Integer id) {
        materialRequestService.deleteMaterialRequest(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}